package com.qys.livesMall.shop.controller.app;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.shop.modal.dto.shop.AppPurchase;
import com.qys.livesMall.shop.modal.entity.ShopPurchase;
import com.qys.livesMall.shop.modal.entity.ShopPurchaseInfo;
import com.qys.livesMall.shop.modal.entity.ShopPurchaseSku;
import com.qys.livesMall.shop.modal.query.QueryPurchase;
import com.qys.livesMall.shop.service.ShopPurchaseInfoService;
import com.qys.livesMall.shop.service.ShopPurchaseSkuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *采购
 * @author
 * @since 2023-06-06
 */
@Tag(name = "采购", description = "采购")
@RestController
@RequestMapping("/app/shopPurchaseInfo")
public class AppShopPurchaseInfoController {
    @Resource
    private ShopPurchaseInfoService purchaseInfoService;

    @Resource
    private ShopPurchaseSkuService purchaseSkuService;


    /**
     * 采购
     * @param appPurchase
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody AppPurchase appPurchase){
        return purchaseInfoService.addList(appPurchase);
    }

    @PostMapping("/page")
    public R<Page<ShopPurchaseInfo>> getList(@RequestBody QueryPurchase data){
        Page<ShopPurchaseInfo> page = Page.of(data.getCurrent(), data.getSize());
        LambdaQueryWrapper<ShopPurchaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(data.getShopId())){
            queryWrapper.eq(ShopPurchaseInfo::getShopId,data.getShopId());
        }
        Page<ShopPurchaseInfo> info = purchaseInfoService.page(page, queryWrapper);

        return R.ok(info);
    }
    @Operation(description = "发票列表")
    @PostMapping("/Invoicepage")
    public R<Page<ShopPurchase>> getInvoiceList(@RequestBody QueryPurchase data){
        Page<ShopPurchaseInfo> page = Page.of(data.getCurrent(), data.getSize());
        LambdaQueryWrapper<ShopPurchaseInfo> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isNotEmpty(data.getShopId())){
            queryWrapper.eq(ShopPurchaseInfo::getShopId,data.getShopId());
        }
        if (data.isMoney()){
            queryWrapper.orderByDesc(ShopPurchaseInfo::getMoneys);
        }
        if (data.isTime()){
            queryWrapper.orderByDesc(ShopPurchaseInfo::getEndTime);
        }
        Page<ShopPurchaseInfo> info = purchaseInfoService.page(page, queryWrapper);
        Page<ShopPurchase> purchasePage = new Page<>();
        List<ShopPurchase> purchaseList = new ArrayList<>();
        BeanUtil.copyProperties(info,purchasePage);
        for (ShopPurchaseInfo record : info.getRecords()) {
            ShopPurchase purchase = new ShopPurchase();
            purchase.setInfo(record);
            List<ShopPurchaseSku> list = purchaseSkuService.list(new LambdaQueryWrapper<ShopPurchaseSku>()
                    .eq(ShopPurchaseSku::getOrderId, record.getId()));
            purchase.setSku(list);
            purchaseList.add(purchase);
        }
        purchasePage.setRecords(purchaseList);
        return R.ok(purchasePage);
    }

    @GetMapping("/{id}")
    public R<ShopPurchase> get(@PathVariable Long id){
        ShopPurchaseInfo shopPurchaseInfo=purchaseInfoService.getById(id);
        List<ShopPurchaseSku> list = purchaseSkuService.list(new LambdaQueryWrapper<ShopPurchaseSku>().eq(ShopPurchaseSku::getOrderId, id));
        ShopPurchase shopPurchase = new ShopPurchase();
        shopPurchase.setInfo(shopPurchaseInfo);
        shopPurchase.setSku(list);
        return R.ok(shopPurchase);
    }

}
